package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.common.contants.MQConstants;
import com.heima.common.dtos.AppHttpCodeEnum;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.article.dtos.UpdateArticleMsg;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.utils.common.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class ApReadBehaviorServiceImpl implements ApReadBehaviorService {
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        //校验参数
        if (dto.getArticleId() == null){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //推送信息到kafka
        //发送消息给KafkaStream进行数据同步
        UpdateArticleMsg articleMsg = new UpdateArticleMsg();
        articleMsg.setArticleId(dto.getArticleId());
        articleMsg.setType(UpdateArticleMsg.UpdateArticleType.VIEWS);
        kafkaTemplate.send(MQConstants.HOT_ARTICLE_INPUT_TOPIC, JsonUtils.toString(articleMsg));

        return ResponseResult.okResult(null);
    }
}
